'use strict';

angular.module('app')
	.controller('menubtnController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
	                                function($rootScope, $scope, $http, $state,$timeout) {

        $scope.loading = false;
		var id = $state.params.id;
		$scope.search = function () {
            $scope.menuId=id;
            validate();
            $scope.firstApply1 = false;
	        $scope.loading = true;
			$.ajax({
				url : $scope.app.project_name+'/menubtn/read/list',
				data: {"menuId":id}
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data.list;
					$scope.menuinfo=result.data.sysMenu;
                    $scope.title = '二级菜单('+result.data.sysMenu.menuName+')按钮管理';
					console.log($scope.menuinfo);
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		$scope.search();
        $scope.disableItem=function(btnId,enable){
                    $scope.loading = true;
                    $.ajax({
                        url : $scope.app.project_name + '/menubtn/delete',
                        data: {'btnId': btnId,"enable":enable}
                    }).then(function(result) {
                        $scope.loading = false;
                        if (result.httpCode == 200) {
                            $scope.search();
                        } else {
                            alert("请求失败");
                        }
                        $scope.$apply();
                    });

        }
		  $scope.approveService = function(){
		$scope.firstApply1 = true;
              $scope.btnId="";
              $scope.btnName="";
              $scope.permission="";
		   }
                                        $scope.approveupdate = function(id,btnName,permission){
                                            $scope.firstApply1 = true;
                                            $scope.btnId=id;
                                            $scope.btnName=btnName;
                                            $scope.permission=permission;

                                    }
                                        function validate() {
                                            jQuery("form").validate({
                                                rules: {
                                                    permission: {
                                                        required: true,
                                                        maxlength:10
                                                    },
                                                    btnName: {
                                                        required: true,
                                                        maxlength:10
                                                    }
                                                },
                                                messages: {
                                                    permission: {
                                                        required: '权限标识不能为空',
                                                        maxlength: '权限标识长度不可超过10'
                                                    },
                                                    btnName: {
                                                        required: '按钮名称不能为空',
                                                        maxlength: '任务名称长度不可超过10'
                                                    }
                                                },
                                                submitHandler: function () {
                                                    $scope.submit();
                                                }
                                            });
                                        }
                                        $scope.submit=function(){
                                            var param={"btnId":$scope.btnId,"btnName":$scope.btnName,"permission":$scope.permission,"menuId":$scope.menuId};
                                            var m = param;
                                            console.log(param);
                                            if(m){
                                                $scope.isDisabled = true;//提交disabled
                                                $.ajax({
                                                    url : $scope.btnId? $scope.app.project_name + '/menubtn/update' : $scope.app.project_name + '/menubtn/add',
                                                    data: param
                                                }).then(callback);
                                            }

                                            function callback(result){
                                                console.log(result);
                                                $scope.isDisabled = false;
                                                if(result.httpCode ==200){//成功
                                                    $scope.isDisabled = false;
                                                    $scope.search();
                                                }else{
                                                    $scope.isDisabled = false;
                                                    alert("请求失败");
                                                }
                                                $scope.$apply();
                                            }
                                        }
} ]);